﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

namespace Frw
{
    public class GenericClassFactory<I, C>
        where C : I
    {
        public static Func<object[], I> Dispenser { get; set; }
        public static Func<I> Dispenser1 { get; set; }

        public static I CreateInstance(params object[] args)
        {
            I retValue = default(I);

            //if (Dispenser != null)
            //    return Dispenser(args);

            retValue = BuildUpContainer();

            return (I)Activator.CreateInstance(typeof(C), args);
        }

        public static I CreateInstance(string resolvingName, params object[] args)
        {
            I retValue = default(I);

            //if (Dispenser != null)
            //    return Dispenser(args);

            retValue = BuildUpContainer(resolvingName);

            return (I)Activator.CreateInstance(typeof(C), args);
        }

        public static I CreateInstance()
        {
            I retValue = default(I);

            //if (Dispenser1 != null)
            //    return Dispenser1();

            retValue = BuildUpContainer();

            return retValue;
        }

        public static I CreateInstance(string resolvingName)
        {
            I retValue = default(I);

            //if (Dispenser1 != null)
            //    return Dispenser1();

            retValue = BuildUpContainer(resolvingName);

            return retValue;
        }

        private static I BuildUpContainer()
        {
            I retValue = default(I); 

            try
            {
                object section = ConfigurationManager.GetSection("unity");

                if (section != null)
                {
                    IUnityContainer container = new UnityContainer();
                    container.LoadConfiguration();

                    retValue = container.Resolve<I>("UserAuthentication");
                }
                else
                {
                    retValue = (I)Activator.CreateInstance(typeof(C));
                }

            }
            catch (Exception ex)
            {
                retValue = (I)Activator.CreateInstance(typeof(C));
            }

            return retValue;
        }

        private static I BuildUpContainer(string resolvingName)
        {
            I retValue = default(I);

            try
            {
                object section = ConfigurationManager.GetSection("unity");

                if (section != null)
                {
                    IUnityContainer container = new UnityContainer();
                    container.LoadConfiguration();

                    retValue = container.Resolve<I>(resolvingName);
                }
                else
                {
                    retValue = (I)Activator.CreateInstance(typeof(C));
                }

            }
            catch (Exception ex)
            {
                retValue = (I)Activator.CreateInstance(typeof(C));
            }

            return retValue;
        }
    }
}
